//入口文件
import Vue from 'vue'
//导入路由的包
import VueRouter from 'vue-router'
//安装路由
Vue.use(VueRouter)

//导入vuex
import Vuex from 'vuex'
Vue.use(Vuex)

var car=JSON.parse(localStorage.getItem('car')||'[]')
var store = new Vuex.Store({
	state: { //this.$store.state.***
		car: car, //保存购物车中的商品数据
		//{id:商品的id,count:要购买的数量，price:商品的单价，selected:false}
	},
	mutations: { //this.$store.commit('方法的名称'，'按需传递唯一参数')
		addToCar(state, goodsinfo) {
			//如果在购物车中没有找到
			var flag = false
			state.car.some(item => {
				if (item.id == goodsinfo.id) {
					item.count += parseInt(goodsinfo.count)
					flag = true
					return true
				}
			})

			// 如果最终，循环完毕，得到的 flag 还是 false，则把商品数据直接 push 到 购物车中
			if (!flag) {
				state.car.push(goodsinfo)
			}
			localStorage.setItem('car',JSON.stringify(state.car));
		},
		updataGoodsInfo(state,goodsinfo){
			state.car.some(item => {
				if (item.id == goodsinfo.id) {
					item.count= parseInt(goodsinfo.count)
					return true
				}
			})
			localStorage.setItem('car',JSON.stringify(state.car));
		},
		removeFormCar(state,id){
			state.car.some((item,index)=>{
				if(item.id==id){
					state.car.splice(index,1);
					return true
				}
			})
			localStorage.setItem('car',JSON.stringify(state.car));
		},
		updataSelected(state,info){
			state.car.some(item=>{
				if(item.id==info.id){
					item.selected=info.selected;
					return true
				}
			})
			localStorage.setItem('car',JSON.stringify(state.car));
		}
	},
	getters: { //this.$store.getters.***
		getAllcount(state) {
			var c = 0;
			state.car.forEach(item => {
				c += parseInt(item.count)
			})
			return c
		},
		getGoodsCount(state){
			var i={};
			state.car.forEach(item=>{
				i[item.id]=item.count;  
			})
			return i
		},
		getSelected(state){
			var o={};
			state.car.forEach(item=>{
				o[item.id]=item.selected
			})
			return o
		},
		getAllSum(state){
			var n={
				count:0,
				price:0
			}
			state.car.forEach(item=>{
				if(item.selected){
					n.count+=item.count;
					n.price+=parseInt(item.price*item.count)
				}
			})
			return n
		}
	},
})

//导入MUI样式
import './lib/mui/css/mui.min.css'
import './lib/mui/css/icons-extra.css'


//导入router.js路由模块
import router from './router.js'


// import { Header,Swipe, SwipeItem,Lazyload} from 'mint-ui';
// Vue.component(Header.name, Header);
// Vue.component(Swipe.name, Swipe);
// Vue.component(SwipeItem.name, SwipeItem);
// Vue.use(Lazyload);
import MintUI from 'mint-ui'
Vue.use(MintUI)
import 'mint-ui/lib/style.css'


// import axios from 'axios'
// Vue.prototype.$axios = axios 


import VueResource from 'vue-resource'
Vue.use(VueResource)
Vue.http.options.root = 'http://api.cms.liulongbin.top';
//全局设置post时候表单数据格式组织形式
Vue.http.options.emulateJSON = true;

//导入格式化时间插件
import moment from 'moment'
//定义全局过滤器
Vue.filter('dateFormat', function(dataStr, pattern = "YYYY-MM-DD hh:mm:ss") {
	return moment(dataStr).format(pattern)
})

//导入图片预览插件
import VuePreview from 'vue-preview'
// defalut install
Vue.use(VuePreview)


//导入App跟组件
import app from './App.vue'
var vm = new Vue({
	el: '#app',
	render: c => c(app),
	router, //
	store
})
